<template>
  <a-modal
    :open="open"
    :width="640"
    title="单项打折"
    centered
    :closable="false"
    ok-text="确认打折"
    :ok-button-props="{ disabled: !isCompleted }"
    @ok="onOk"
    @cancel="onCancel"
  >
    <div class="flex justify-between ml-5">
      <div class="w-30 mr-2">
        {{ formState.xmmc }}<span class="text-gray-400 ml-3">{{ formState.sl }}{{ formState.dw }}</span>
      </div>
      <div class="text-red-500">￥{{ formatAmount(formState.bzdjShow) }}</div>
    </div>
    <a-divider class="!my-2.5" />
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="折扣率%" class="w-1/2" v-bind="validateInfos.zkl">
        <a-input v-model:value="formState.zkl" v-input:float.2 placeholder="请输入" @blur="onBlurDiscount" />
      </a-form-item>
      <a-form-item :label="isDiscount ? '减免金额' : '结算金额'" class="w-1/2" v-bind="validateInfos.sjdj">
        <a-input v-model:value="formState.sjdj" v-input:float.2 placeholder="请输入" @blur="onBlurPrice" />
      </a-form-item>
      <a-divider class="m-0 mb-2.5" />
      <a-form-item label="批准人" class="w-1/2" v-bind="validateInfos.pzr">
        <a-input v-model:value="formState.pzr" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="密码" class="w-1/2" v-bind="validateInfos.pwd">
        <a-input-password v-model:value="formState.pwd" placeholder="请输入密码" autocomplete="new-password" :max-length="20" />
        <template #prefix>
          <LockOutlined class="text-primary text-base" />
        </template>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { LockOutlined } from '@ant-design/icons-vue'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const isDiscount = getSystemConfig('DISCOUNT_INPUT_MODE') === '1' // 0:实收金额|1:减免金额

const formState = ref({
  zkl: '',
  sjdj: '',
  pzr: '',
  pwd: ''
})
const rulesState = ref({
  zkl: [{ required: true, message: '请输入折扣率' }],
  sjdj: [{ required: true, message: '请输入结算金额' }]
  // pzr: [{ required: true, message: '请输入批准人' }],
  // pwd: [{ required: true, message: '请输入密码' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onBlurDiscount = () => {
  if (Number(formState.value.zkl) && Number(formState.value.zkl) > 100) {
    formState.value.zkl = 100
    return message.error('不能大于100')
  }
  if (isDiscount) {
    if (+formState.value.zkl || +formState.value.zkl === 0) {
      formState.value.sjdj = formatAmount(Number(formState.value.bzdjShow) - Number(formState.value.bzdjShow) * (Number(formState.value.zkl) / 100))
    }
  } else {
    if (+formState.value.zkl || +formState.value.zkl === 0) {
      formState.value.sjdj = formatAmount((Number(formState.value.zkl) / 100) * Number(formState.value.bzdjShow))
    }
  }

  // if (Number(formState.value.zkl) && Number(formState.value.zkl) < props.rowData.empZkl) {
  //   formState.value.zkl = ''
  //   return message.error('不能超过最大折扣率')
  // }
}
const onBlurPrice = () => {
  if (isDiscount) {
    if (+formState.value.sjdj) {
      formState.value.zkl = formatAmount(((Number(formState.value.bzdjShow) - Number(formState.value.sjdj)) / Number(formState.value.bzdjShow)) * 100)
    }
  } else {
    if (+formState.value.sjdj) {
      formState.value.zkl = formatAmount((Number(formState.value.sjdj) / Number(formState.value.bzdjShow)) * 100)
    }
  }
}

const price = ref(0)
const onOk = async () => {
  const res = await post('/operation/opex/getDiscountPermisson', { empId: formState.value.pzr })
  if (res.code !== 0) return message.error(res.msg)
  // if (Number(formState.value.zkl) < res.data.discountPercent) {
  //   return message.error('不能超过最大折扣率')
  // }
  if (Number(formState.value.zkl) < res.data.discountPercent) {
    return message.error(`当前您可打的最高折扣为<${res.data.discountPercent}%>`)
  }
  if (isDiscount) {
    if (Number(formState.value.sjdj) > res.data.maxDiscountAmount) {
      return message.error(`当前您的最高减免金额为<${formatAmount(res.data.maxDiscountAmount)}>元`)
    }
  } else {
    if (Number(formState.value.bzdjShow) - Number(formState.value.sjdj) > res.data.maxDiscountAmount) {
      return message.error(`当前您的最高减免金额为<${formatAmount(res.data.maxDiscountAmount)}>元`)
    }
  }
  if (isDiscount) {
    price.value = formatAmount(Number(formState.value.bzdjShow) - (Number(formState.value.sjdj) || 0))
  }
  const params = {
    hjje: formState.value.je,
    jsje: isDiscount ? price.value : formState.value.sjdj,
    pwd: formState.value.pwd,
    pzr: formState.value.pzr,
    yzids: [formState.value.id]
  }
  emit('success', params)
}
const onCancel = () => {
  emit('update:open', false)
}
watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
      formState.value.bzdjShow = Number(formState.value.sjdj) * Number(formState.value.sl)
      if (!isDiscount) {
        formState.value.sjdj = Number(formState.value.sjdj) * Number(formState.value.sl)
        // formState.value.sjdj = formatAmount(formState.value.bzdj)
        formState.value.zkl = formatAmount((Number(formState.value.sjdj) / Number(formState.value.bzdjShow)) * 100)
        formState.value.sjdj = formatAmount((Number(formState.value.zkl) / 100) * Number(formState.value.bzdjShow))
      } else {
        formState.value.sjdj = ''
        // formState.value.sjdj = formatAmount(formState.value.sjdj)
        // formState.value.zkl = formatAmount(((Number(formState.value.bzdjShow) - Number(formState.value.sjdj)) / Number(formState.value.bzdjShow)) * 100)
      }
    } else {
      resetFields()
    }
  }
)
</script>
<style lang="less" scoped></style>
